'''查看xml的种类和数量'''

import os
import random
import math
import shutil
import xml
import pandas as pd
from pathlib import Path
import xml.etree.ElementTree as ET


images_path = Path('/home/xys/CloundShiProjects/motorbicyle_detect/motor_obj_det_data/mtc/image')
anno_path = Path('/home/xys/CloundShiProjects/motorbicyle_detect/motor_obj_det_data/mtc/xml')
# error_path = '/home/xys/CloundShiProjects/traffic_light/trafficlight_dect/data/error/'

def filelist(root, file_type):

    return [os.path.join(directory_path, f) for directory_path, directory_name,
            files in os.walk(root) for f in files if f.endswith(file_type)]


def generate_train_df (anno_path):
    annotations = filelist(anno_path, '.xml')
    anno_list = []
    num = 0
    for anno_path in annotations:
        num+=1
        print(num)
        root = ET.parse(anno_path).getroot()
        # try:
        #     root = ET.parse(anno_path).getroot()
        # except xml.etree.ElementTree.ParseError:
        #     shutil.copy(anno_path, error_path)
        anno = {}
        anno['filename'] = Path(str(images_path) + '/'+ root.find("./filename").text)
        anno['width'] = root.find("./size/width").text
        anno['height'] = root.find("./size/height").text
        anno['class'] = root.find("./object/name").text
        anno['xmin'] = int(root.find("./object/bndbox/xmin").text)
        anno['ymin'] = int(root.find("./object/bndbox/ymin").text)
        anno['xmax'] = int(root.find("./object/bndbox/xmax").text)
        anno['ymax'] = int(root.find("./object/bndbox/ymax").text)
        anno_list.append(anno)
    return pd.DataFrame(anno_list)
df_train = generate_train_df(anno_path)

print(df_train['class'].value_counts())